/*
 * This file is part of Cadmium.
 * Copyright (C) 2007-2010 Xavier Clerc.
 *
 * Cadmium is free software; you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation; either version 3 of the License, or
 * (at your option) any later version.
 *
 * Cadmium is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package fr.x9c.cadmium.support.values;

import fr.x9c.cadmium.kernel.Block;
import fr.x9c.cadmium.kernel.Value;
import fr.x9c.cadmium.primitives.stdlib.Hash;

/**
 * This class implements a 'polymorphic' variant.
 *
 * @param <T> type of embedded value
 *
 * @author <a href="mailto:cadmium@x9c.fr">Xavier Clerc</a>
 * @version 1.0
 * @since 1.0
 */
public final class PolymorphicVariant<T extends ToValue> implements ToValue {

    /** The variant name. */
    private final String name;

    /** The actual value. */
    private final T value;

    /**
     * Constructs a polymorphic variant.
     * @param n variant name - should not be <tt>null</tt>
     * @param v variant embedded value - should not be <tt>null</tt>
     */
    public PolymorphicVariant(final String n, final T v) {
        assert n != null : "null n";
        assert v != null : "null v";
        this.name = n;
        this.value = v;
    } // end constructor(String, T)

    /**
     * Constructs a polymorphic variant.
     * @param n variant name - should not be <tt>null</tt>
     */
    public PolymorphicVariant(final String n) {
        assert n != null : "null n";
        this.name = n;
        this.value = null;
    } // end constructor(String)

    /**
     * {@inheritDoc}
     */
    @Override
    public Value toValue() {
        if (this.value != null) {
            final Block res = Block.createBlock(0,
                                                Hash.hashVariant(this.name),
                                                this.value.toValue());
            return Value.createFromBlock(res);
        } else {
            return Hash.hashVariant(this.name);
        } // end if/else
    } // end method 'toValue()'

    /**
     * {@inheritDoc}
     */
    @Override
    public int hashCode() {
        return this.name.hashCode()
            + (this.value != null ? this.value.hashCode() : 0);
    } // end method 'hashCode()'

    /**
     * {@inheritDoc}
     */
    @Override
    public boolean equals(final Object obj) {
        if (obj instanceof PolymorphicVariant) {
            final PolymorphicVariant<?> that = (PolymorphicVariant<?>) obj;
            return this.name.equals(that.name)
                && (((this.value == null) && (that.value == null))
                    || ((this.value != null) && this.value.equals(that.value)));
        } else {
            return false;
        } // end if/else
    } // end method 'equals(Object)'

    /**
     * {@inheritDoc}
     */
    @Override
    public String toString() {
        final StringBuilder sb = new StringBuilder("[");
        sb.append("`");
        sb.append(this.name);
        if (this.value != null) {
            sb.append("(");
            sb.append(this.value.toString());
            sb.append(")");
        } // end if
        return sb.toString();
    } // end method 'toString()'

} // end interface 'PolymorphicVariant'
